package com.example.hellowordl;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;

public class InterestSelectionActivity extends AppCompatActivity {

    private TextView btnBack;
    private TextView btnConfirm;
    private CheckBox cbReading;
    private CheckBox cbPhotography;
    private CheckBox cbPingpong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_selection);

        // 初始化控件
        btnBack = findViewById(R.id.btn_back);
        btnConfirm = findViewById(R.id.btn_confirm);
        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingpong = findViewById(R.id.cb_pingpong);

        // 设置点击监听器
        setupListeners();
    }

    private void setupListeners() {
        // 返回按钮点击事件：关闭当前页面
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 确认按钮点击事件：收集选中的兴趣并返回结果
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnSelectedInterests();
            }
        });
    }

    private void returnSelectedInterests() {
        ArrayList<String> interests = new ArrayList<>();
        // 检查每个复选框是否被选中，收集兴趣
        if (cbReading.isChecked()) {
            interests.add("阅读");
        }
        if (cbPhotography.isChecked()) {
            interests.add("摄影");
        }
        if (cbPingpong.isChecked()) {
            interests.add("乒乓球");
        }

        // 拼接选中的兴趣为字符串
        StringBuilder resultBuilder = new StringBuilder();
        for (int i = 0; i < interests.size(); i++) {
            resultBuilder.append(interests.get(i));
            if (i < interests.size() - 1) {
                resultBuilder.append("、");
            }
        }
        String resultString = resultBuilder.toString();

        // 封装结果并返回给上一个页面
        Intent resultIntent = new Intent();
        resultIntent.putExtra("INTEREST_RESULT_KEY", resultString);
        setResult(RESULT_OK, resultIntent);
        finish(); // 关闭当前页面
    }
}